<?php
require_once '../db_connection.php';

class SinhVien {
	
	public function logIn($tendangnhap, $matkhau){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select * from `TAIKHOAN` where TENDANGNHAP='" .$tendangnhap."' and MATKHAU='" .$matkhau."'
				and QUYEN=1");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	/**
	 * lay ra ten sinh vien khi dang nhap vao he thong
	 */
	public function getTenSV($taikhoan){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("SELECT HOTENSV FROM SINHVIEN a join TAIKHOAN b on a.MASOSV = b.MASOSV
			WHERE b.MASOSV = '".$taikhoan."';");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	
	/**
	 * Lay ra cac nam hoc
	 */
	public function getNamHoc($masv){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("SELECT NHOC FROM HOC where MASOSV =" .$masv. " group by NHOC;");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	
	/**
	 * Danh sach hoc phan
	 */
	public function getDanhSachHP($nienkhoa, $hocki, $masosv){
		$data = new DatabaseConnect();
		$data->Open();
	
		/* $data->setSql("SELECT MAHP, KIHIEU FROM SQA.GIANGDAY where MASOGV = '".$masogv."' and HKI = '".$hocki."' and NHOC = '".$nienkhoa."'"); */
		$data->setSql("select a.MAHP, TENHP, a.KIHIEU, THU, TIET, PHONG, a.HKI, a.NHOC from TKB a join HOC b on a.MAHP = b.MAHP
 			and a.HKI = b.HKI where b.HKI = '".$hocki."' and b.NHOC = '".$nienkhoa."' and b.MASOSV = '".$masosv."'
			group by MAHP;");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	/**
	 * lay noi dung nhat ki
	 */
	
	public function getNhatKi($mahocphan, $nhomhp, $hocki, $namhoc, $masv){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select THOIGIAN, NOIDUNG_SV from SONHATKI where MAHP='".$mahocphan."'
				and KIHIEU='".$nhomhp."' and HKI='".$hocki."' and NHOC='".$namhoc."' and MASOSV='".$masv."'");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	/**
	 * ghi nhat ki
	 */
	public function setNhatKi($mahocphan, $nhomhp, $hocki, $namhoc, $masv, $noidung, $thoigian){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("insert into SONHATKI(MAHP, KIHIEU, HKI, NHOC, MASOSV, NOIDUNG_SV, TRANGTHAI, THOIGIAN)
			values('".$mahocphan."', '".$nhomhp."', '".$hocki."', '".$namhoc."', '".$masv."',
				'".$noidung."', '2', '".$thoigian."');");
		$result = $data->ExecuteQuery();
		$data->Close();
		return $result;
	}
	/**
	 * Cap nhat lai phan nhat ki cua sinh vien
	 * khi da co giang vien ghi truoc noi dung
	 */
	public function updateNhatKi($mahocphan, $nhomhp, $hocki, $namhoc, $masv, $noidungnhap, $date){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("update SONHATKI set MASOSV='".$masv."', NOIDUNG_SV='".$noidungnhap."', TRANGTHAI=0
			where MAHP='".$mahocphan."' and KIHIEU='".$nhomhp."' and NHOC='".$namhoc."' and THOIGIAN='".$date."'");
		$data->ExecuteQuery();
		$data->Close();
	}
	/**
	 * Lay trang thai cua nhat ki
	 */
	public function getTrangThai($mahocphan, $nhomhocphan, $hocki, $namhoc, $masv, $date){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select TRANGTHAI FROM SONHATKI where MAHP='".$mahocphan."' and KIHIEU='".$nhomhocphan."'
				and HKI='".$hocki."' and NHOC='".$namhoc."'
 				and MASOSV='".$masv."' and THOIGIAN='".$date."'");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	/**
	 * Lay ma truy cap hoc phan
	 */
	public function getMaTruyCap($mahocphan, $nhomhocphan, $hocki, $namhoc){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select MATRUYCAP FROM GIANGDAY where MAHP='".$mahocphan."'
				 and KIHIEU='".$nhomhocphan."' and HKI=".$hocki." and NHOC='".$namhoc."'");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	
	/**
	 * Kiem tra xem ngay nhap nhat ki co ton tai hay chua
	 */
	public function testDate($date){
		$changeDate = date("Y-m-d", strtotime($date));
		//echo $changeDate;
		$test = false;
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select THOIGIAN FROM SONHATKI;");
		$results = $data->LoadArray();
		$data->Close();
		foreach ($results as $result){
			//echo date("Y-m-d",strtotime($result[0]));
			if($changeDate == date("Y-m-d",strtotime($result[0]))){
				$test = true;
			}
		}
		return $test;
	}
	
	/**
	 * Kiem tra xem noi dung co duoc ghi hay chua
	 */
	public function getNoiDungSV($mahocphan, $nhomhocphan, $hocki, $namhoc, $masv, $date){
	
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select NOIDUNG_SV FROM SONHATKI WHERE MAHP='".$mahocphan."' and KIHIEU='".$nhomhocphan."' and NHOC='".$namhoc."' 
				and HKI='".$hocki."'and THOIGIAN='".$date."' and MASOSV='".$masv."'");
		/* echo "select NOIDUNG_SV FROM SONHATKI WHERE MAHP='".$mahocphan."' and KIHIEU='".$nhomhocphan."' and NHOC='".$namhoc."' 
				and HKI='".$hocki."'and THOIGIAN='".$date."' and MASOSV='".$masv."'"; */
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	/**
	 * Lay noi dung sinh vien
	 */
	public function getNoiDungGV($mahocphan, $nhomhocphan, $hocki, $namhoc, $date){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select NOIDUNG_GV FROM SONHATKI WHERE MAHP='".$mahocphan."' and KIHIEU='".$nhomhocphan."'
				and NHOC='".$namhoc."' and HKI='".$hocki."' and THOIGIAN='".$date."' ");
		/* echo "select NOIDUNG_GV FROM SONHATKI WHERE MAHP='".$mahocphan."' and KIHIEU='".$nhomhocphan."'
				and NHOC='".$namhoc."' and HKI='".$hocki."' and THOIGIAN='".$date."' "; */
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	
}